<template>
  <div>
    <el-card style="margin-bottom:20px;">
      <div slot="header" class="clearfix">
        <span>关于我</span>
      </div>

      <div class="user-profile">
        <div class="box-center">
          <pan-thumb :image="user.avatar" :height="'100px'" :width="'100px'" :hoverable="false">
            <div>Hello</div>
            {{ user.role }}
          </pan-thumb>
        </div>
        <div class="box-center">
          <div class="user-name text-center">{{ user.username }}</div>
          <div class="user-role text-center text-muted">{{ user.role | uppercaseFirst }}</div>
          <el-button class="user-btn" type="primary" @click="handleUpdatePassword">修改密码</el-button>
        </div>
      </div>

      <!-- <div class="user-bio">
      <div class="user-education user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="education" /><span>Education</span></div>
        <div class="user-bio-section-body">
          <div class="text-muted">
            JS in Computer Science from the University of Technology
          </div>
        </div>
      </div>

      <div class="user-skills user-bio-section">
        <div class="user-bio-section-header"><svg-icon icon-class="skill" /><span>Skills</span></div>
        <div class="user-bio-section-body">
          <div class="progress-item">
            <span>Vue</span>
            <el-progress :percentage="70" />
          </div>
          <div class="progress-item">
            <span>JavaScript</span>
            <el-progress :percentage="18" />
          </div>
          <div class="progress-item">
            <span>Css</span>
            <el-progress :percentage="12" />
          </div>
          <div class="progress-item">
            <span>ESLint</span>
            <el-progress :percentage="100" status="success" />
          </div>
        </div>
      </div>
    </div>-->
    </el-card>
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form ref="temp" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">

        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" :disabled="true" />
        </el-form-item>

        <el-form-item label="原始密码" prop="oldPassword">
          <el-input v-model="temp.oldPassword" placeholder="原始密码" />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="temp.newPassword" placeholder="新密码" />
        </el-form-item>

        <el-form-item label="确认密码" prop="newEnterPassword">
          <el-input v-model="temp.newEnterPassword" placeholder="确认密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="updateData">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PanThumb from '@/components/PanThumb'
import { updateUser } from '@/api/user'

export default {
  components: { PanThumb },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          username: '',
          avatar: '',
          role: ''
        }
      }
    }
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.newEnterPassword !== '') {
          this.$refs.temp.validateField('newEnterPassword')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入确认密码'))
      } else if (value !== this.temp.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    var validatePass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入确原始密码'))
      } else {
        callback()
      }
    }
    return {
      dialogStatus: false,
      dialogFormVisible: false,
      dialogTitle: '修改',
      temp: {
        username: '',
        oldPassword: '',
        newPassword: '',
        newEnterPassword: ''
      },
      rules: {
        oldPassword: [{ required: true, validator: validatePass3, trigger: 'blur' }],
        newPassword: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        newEnterPassword: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    resetTemp() {
      this.temp = {
        username: '',
        oldPassword: '',
        newPassword: '',
        newEnterPassword: ''
      }
    },
    handleUpdatePassword() {
      this.resetTemp()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['temp'].clearValidate()
      })
    },
    updateData() {
      this.$refs['temp'].validate((valid) => {
        if (valid) {
          this.temp.username = this.user.username
          updateUser(this.temp).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改完成',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    created() {

    }
  }
}
</script>

<style lang="scss" scoped>
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }
  .user-btn {
    padding-top: 10px;
  }
  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
